// pages/myinfo/myinfo.js
let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
   data: {
    headImg:'',
    userInfo:{
      avatarUrl: '../images/timg.jpeg',
      nickName: '匿名用户',
    },
    hasUserInfo: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
   inputChangge(e){
    let key = e.currentTarget.dataset.key;
    if (key == 'userName') {
      this.setData({'userInfo.userName':e.detail.value})
    }
    if (key == 'phoneNum') {
      this.setData({'userInfo.phoneNum':e.detail.value})
    }
    if (key == 'carType') {
      this.setData({'userInfo.carType':e.detail.value})
    }

   },
   changeIcon(){
    let self = this;
    wx.chooseImage({
      count: 1, 
      sizeType: ['original', 'compressed'], 
      sourceType: ['album', 'camera'], 
      success: function (res) {

        var tempFilePaths = res.tempFilePaths
        self.setData({'userInfo.avatarUrl': tempFilePaths[0]})


        wx.uploadFile({
          url: app.globalData.serverUrl+'CarShareMessage/upLoadImages.do', 
          filePath: tempFilePaths[0],
          name: 'file',
          formData:{
            'openId': app.globalData.serverOpenInfo.openid
          },
          success: function(res){
            var data = JSON.parse(res.data)
            if (data.state == 1) {
              console.log(data)
              wx.showToast({title: '上传成功',icon: 'none'});

              self.setData({
                headImg :data.data.localFilePath
              })
            }


          }
        })

      }
    })
  },
  upDataMyinfo(){
    wx.showLoading({title:'正在提交'})
    let userInfo = this.data.userInfo;
    if (userInfo.phoneNum.length == 11) {
      wx.request({
        url:app.globalData.serverUrl + 'myMessage/updateMyMessage.do',
        data:{
          openId: app.globalData.serverOpenInfo.openid,
          userName:userInfo.userName || userInfo.nickName,
          phoneNum:userInfo.phoneNum,
          carType: userInfo.carType || false,
          headImage: this.data.headImg
        },
        success(res){
          console.log(res)
          if (res.data.state == 1) {
            app.getServerUserInfo();
            wx.hideLoading()

            wx.showToast({title: '更新成功',icon: 'none'});
            setTimeout(function(){
              wx.switchTab({ url: '../index/index'})
            },1500)
          }else{
            wx.hideLoading()
            wx.showToast({title: '更新失败',icon: 'none'});

            wx.switchTab({ url: '../index/index'})
          }
        }
      })

    }else{
      wx.showToast({
        title: '请输入正确的手机号码！',
        icon: 'success',
        duration: 2000
      })
    }



    
  },
  onLoad: function (options) {


    let self = this;
    app.sendUserInfoPage(function(data){
      if (data) {
        console.log(data)
        self.setData({
          userInfo: data,
          hasUserInfo: true
        })
      }
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
   onReady: function () {

   },

  /**
   * 生命周期函数--监听页面显示
   */
   onShow: function () {

   },

  /**
   * 生命周期函数--监听页面隐藏
   */
   onHide: function () {

   },

  /**
   * 生命周期函数--监听页面卸载
   */
   onUnload: function () {

   },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
   onPullDownRefresh: function () {

   },

  /**
   * 页面上拉触底事件的处理函数
   */
   onReachBottom: function () {

   },

  /**
   * 用户点击右上角分享
   */
   onShareAppMessage: function () {

   }
 })